package com.cn.wx.dao.impl;

import com.cn.wx.dao.NewsDao;
import com.cn.wx.model.NewsModel;
import com.cn.wx.model.UserModel;
import com.cn.wx.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsImpl implements NewsDao {
    @Override
    public List<NewsModel> getAll(Integer pageSize, Integer pageNum) {
        String sql = "select * from news limit " + (pageNum - 1) * pageSize + "," + pageSize;
        List<NewsModel> list = new ArrayList<>();
        ResultSet resultSet = JDBC.getResultSet(sql);

        try {
            while (resultSet.next()) {
                NewsModel newsModel = new NewsModel();
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setIntroduction(resultSet.getString("introduction"));
                newsModel.setCreate_time(resultSet.getString("create_time"));
                list.add(newsModel);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }


        return list;
    }

    @Override
    public int count(String string) {
        String sql = "select count(*) as count  from news";
        ResultSet resultSet = JDBC.getResultSet(sql);
        String count = "-1";
        try {
            while (resultSet.next()) {
                count = resultSet.getString("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }
        return Integer.parseInt(count);
    }

    @Override
    public NewsModel getUserById(Integer id) {
        String sql = "select * from news where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        NewsModel newsModel = null;
        try {
            while (resultSet.next()) {
                newsModel = new NewsModel();
                newsModel.setId(resultSet.getInt("id"));
                newsModel.setTitle(resultSet.getString("title"));
                newsModel.setIntroduction(resultSet.getString("introduction"));
                newsModel.setCreate_time(resultSet.getString("create_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBC.close();
        }

        return newsModel;
    }

    @Override
    public int update(NewsModel newsModel) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String sql = "update news set title='" + newsModel.getTitle() + "', " +
                " introduction='" + newsModel.getIntroduction() + "' ," +
                " create_time='" + simpleDateFormat.format(new Date()) + "'" +
                " where id=" + newsModel.getId();
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public int delete(Integer id) {
        String sql = "delete from news where id=" + id;
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public int add(NewsModel newsModel) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String sql = "insert into news(title,introduction,create_time)values('" +
                newsModel.getTitle() + "','" + newsModel.getIntroduction() + "'," +
                "'" + simpleDateFormat.format(new Date()) + "')";
        return JDBC.excuteUpdate(sql);
    }

}
